﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace BlogTruyen.ViewModels
{
    public class Search : Models.BaseModel
    {
        public ObservableCollection<Models.Story> items { get; set; }
        public bool isLoaded { get; private set; }
        public string nextPage { get; set; }
        public Search()
        {
            items = new ObservableCollection<Models.Story>();
            nextPage = "1";
        }
        public void LoadHtml(string html)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            //API.TrySaveFile("1.txt", html);
            var sections = doc.DocumentNode.SelectNodes("//div[@class='hidden tiptip-content']");
            if (sections == null)
            {
                this.error = true;
                return;
            }
            var listNode = doc.DocumentNode.SelectSingleNode("//div[@class='list']");
            var pNodes = listNode.SelectNodes("./p");
            HtmlNode titleNode, summaryNode, imgNode;
            int i = 1;
            foreach (HtmlNode section in sections)
            {
                titleNode = section.SelectSingleNode("./div[@class='bold al-c fs-12']");
                summaryNode = section.SelectSingleNode("./div[@class='al-j fs-12']");
                imgNode = section.SelectSingleNode("./img");
                items.Add(new BlogTruyen.Models.Story
                {
                    chapter_count = pNodes[i].SelectNodes("./span[@class='fs-12']")[0].InnerText.Trim(),
                    title = HttpUtility.HtmlDecode(titleNode.InnerText).Trim(),
                    summary = HttpUtility.HtmlDecode(summaryNode.InnerText).Trim(),
                    cover_img = imgNode.GetAttributeValue("src", ""),
                    url = pNodes[i].SelectSingleNode(".//a").GetAttributeValue("href", ""),
                });
                i++;
            }

            //next page
            nextPage = null;
            var pagingHome = doc.DocumentNode.SelectSingleNode("//div[@class='paging']");
            if (pagingHome != null)
            {
                var currentPage = pagingHome.SelectSingleNode("./span[@class='current_page']");
                var pageNodes = pagingHome.SelectNodes("./span");
                var currentPageIndex = pageNodes.IndexOf(currentPage);

                if (currentPageIndex < pageNodes.Count - 1)
                {
                    nextPage = pageNodes[currentPageIndex + 1].SelectSingleNode("./a").InnerText.Trim();
                }
            }
            this.error = false;
            isLoaded = true;
        }

        private bool _error;
        public bool error
        {
            get { return _error; }
            set
            {
                _error = value;
                NotifyPropertyChanged("error");
            }
        }
    }
}
